package com.wangp.mywrite.coding;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: wp
 * @date: 2023/5/30 11:28:32
 * @desc:
 **/
public class DelNodes {

    public List<TreeNode> delNodes(TreeNode root, int[] to_delete) {
        List<Integer> list = new ArrayList<>();
        for (int i : to_delete) {
            list.add(i);
        }

        List<TreeNode> res = new ArrayList<>();
        getNodes(res,true, root, list);
        return res;
    }

    private TreeNode getNodes(List<TreeNode> res, boolean isRoot, TreeNode root, List<Integer> del) {
        if(root == null){
            return null;
        }
        boolean deleted = del.contains(root.val);
        root.left = getNodes(res,deleted,root.left,del);
        root.right = getNodes(res,deleted,root.right,del);
        if(deleted){
            return null;
        }else {
            if(isRoot){
                res.add(root);
            }
            return root;
        }
    }

    public static void main(String[] args) {
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        TreeNode node4 = new TreeNode(4);
        TreeNode node5 = new TreeNode(5);
        TreeNode node6 = new TreeNode(6);
        TreeNode node7 = new TreeNode(7);

        node1.left = node2;
        node1.right = node3;
        node2.left = node4;
        node2.right = node5;
        node3.left = node6;
        node3.right = node7;

        System.out.println(new DelNodes().delNodes(node1, new int[]{3, 5}));
    }
}
